//index.js


const requestToGetHouseBook = require('../../utils/request.js').requestToGetHouseBook
const requestToGetBook = require('../../utils/request.js').requestToGetBook

const toBindBookPage = require('../../utils/wx-navigate.js').toBindBookPage
const toBindPhonePage = require('../../utils/wx-navigate.js').toBindPhonePage
const toBookDeatilPage = require('../../utils/wx-navigate.js').toBookDeatilPage



//获取应用实例
const app = getApp()

Page({
  data: {
    image_add: '/images/icon-add-normal.png',
    
    bookList:[],
    phone:""
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {

    

    this.toGetData()
    app.updateIndexPageData = this.toGetData
    // wx.showTabBarRedDot({
    //   index: 0,
    // })
  },

  toGetData(){
    requestToGetBook("renter_id", res => {
      this.setData({
        bookList: res.list,
        phone: res.phone||null
      })

    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.toGetData()
  },

  

  catchBottomItemTab: function (e) {
    switch (e.detail.tabItem) {
      case -1://
        console.log('点击了新建账本')
        break;
      case 0://
        console.log('点击了租房账本')
        break;
      case 1://
        console.log('点击了我的')
        break;
    }
  },

  _bindimagetouchstart() {
    this.setData({
      image_add: '/images/icon-add-pressed.png',
    })
  },
  _bindimagetouchend() {
    this.setData({
      image_add: '/images/icon-add-normal.png',
    })
  },

  bingimagetap(e){
    console.log('-------')
    console.log(this.data.phone)
    if (this.data.phone) {
      toBindBookPage()
      console.log('toBindBookPagesssss')
    } else {
      // 在没有登陆的情况下要做额外的判断，token为空是，提示
      let token = app.globalData.token
      console.log(token)
      if (token) {
        console.log("没有手机号，需要先绑定手机号")
        toBindPhonePage()
      } else {
        wx.showToast({
          title: "请重新登录",
          icon: 'none',
          mask: true,
        })
      }
    }
  },

  bindbookitemtab(e){
    toBookDeatilPage(e.currentTarget.dataset.book)
  }
})
